package com.library.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 分页插件配置
 *
 * @author: xyh
 * @create: 2023-09-02
 **/
@Configuration
public class MybatisPlusConfig {

    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //MybatisPlusInterceptor对象，配置了两个内部拦截器：分页拦截器和乐观锁拦截器。
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //MyBatis-Plus 提供的分页插件，用于处理分页查询的逻辑。
        PaginationInnerInterceptor innerInterceptor=new PaginationInnerInterceptor();
        //数据库类型为 MySQL
        innerInterceptor.setDbType(DbType.MYSQL);
        //启用溢出处理
        innerInterceptor.setOverflow(true);
        //OptimisticLockerInnerInterceptor乐观锁插件，用于实现乐观锁功能。
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(innerInterceptor);
        return interceptor;
    }
}
